package com.singi.controller;

import com.eova.common.base.BaseController;
import com.jfinal.aop.Clear;
import com.jfinal.core.paragetter.Para;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import java.util.List;

/**
 * Created by Administrator on 2020-2-3.
 */

@Clear
public class InforController extends BaseController {
    private String domain_url="https://www.xinjisx.com";

    public void getAllInfor(@Para("language") String language,@Para("page") int page){
        int page_num = page-1;
        if(language.equalsIgnoreCase("cn")){
            List<Record> infordata =  Db.find("SELECT\n" +
                    "    a.id,\n" +
                    "    a.title,\n" +
                    "    a.thumb,\n" +
                    "    a.brief,\n" +
                    "    a.content,\n" +
                    "    b.enum_value as type,\n" +
                    "    a.create_time\n" +
                    "  FROM\n" +
                    "    xj_information a\n" +
                    "INNER JOIN xj_enum_item b ON a.type = b.id\n" +
                    "WHERE a.title is not null  order by create_time desc limit ?,10",page_num);
            for(int i = 0;i<infordata.size();i++){
                infordata.get(i).set("thumb",domain_url+"/infor/"+infordata.get(i).get("thumb"));
            }
            renderJson(infordata);
        }else{
            List<Record> infordata =  Db.find("SELECT\n" +
                    "a.id,\n" +
                    "a.title_en as title,\n" +
                    "a.thumb,\n" +
                    "a.brief_en as brief,\n" +
                    "a.content_en as conten,\n" +
                    "b.enum_value as type,\n" +
                    "a.create_time\n" +
                    "FROM\n" +
                    "xj_information a\n" +
                    "INNER JOIN xj_enum_item b ON a.type = b.id\n" +
                    "WHERE a.title_en is not null order by create_time desc limit ?,10",page_num);
            for(int i = 0;i<infordata.size();i++){
                infordata.get(i).set("thumb",domain_url+"/infor/"+infordata.get(i).get("thumb"));
            }
            renderJson(infordata);
        }

    }

    public void getTypeInfor(@Para("type_id") String type_id,@Para("language") String language,@Para("page") int page){
        int page_num = page-1;
        if(language.equalsIgnoreCase("cn")){
            List<Record> inforTypedata =  Db.find("SELECT\n" +
                    "\ta.id,\n" +
                    "\ta.title,\n" +
                    "\ta.thumb,\n" +
                    "\ta.brief,\n" +
                    "\ta.content,\n" +
                    "\tb.enum_value,\n" +
                    "\ta.create_time\n" +
                    "FROM\n" +
                    "\txj_information a\n" +
                    "INNER JOIN xj_enum_item b ON a.type = b.id\n" +
                    "where  a.title is not null and b.vcode = ? order by create_time desc limit ?,10",type_id,page_num);
            for(int i = 0;i<inforTypedata.size();i++){
                inforTypedata.get(i).set("thumb",domain_url+"/infor/"+inforTypedata.get(i).get("thumb"));
            }
            renderJson(inforTypedata);
        }else{
            List<Record> inforTypedata =  Db.find("SELECT\n" +
                    "\ta.id,\n" +
                    "\ta.title_en as title,\n" +
                    "\ta.thumb,\n" +
                    "\ta.brief_en as brief,\n" +
                    "\ta.content_en as content,\n" +
                    "\tb.enum_value,\n" +
                    "\ta.create_time\n" +
                    "FROM\n" +
                    "\txj_information a\n" +
                    "INNER JOIN xj_enum_item b ON a.type = b.id\n" +
                    "where a.title_en is not null and  b.vcode = ? order by create_time desc limit ?,10",type_id,page_num);
            for(int i = 0;i<inforTypedata.size();i++){
                inforTypedata.get(i).set("thumb",domain_url+"/infor/"+inforTypedata.get(i).get("thumb"));
            }
            renderJson(inforTypedata);
        }

    }

    public void getInforDetail(@Para("id") int id,@Para("language") String language){
        if(language.equalsIgnoreCase("cn")){
            Record inforDetail =  Db.findFirst("SELECT\n" +
                    "\ta.title,\n" +
                    "\ta.thumb,\n" +
                    "\ta.content,\n" +
                    "\ta.create_time,\n" +
                    "\tb.enum_id\n" +
                    "FROM\n" +
                    "\txj_information a\n" +
                    "INNER JOIN xj_enum_item b ON a.type = b.id\n" +
                    "WHERE\n" +
                    "\ta.id = ?",id);

            inforDetail.set("thumb",domain_url+"/infor/"+inforDetail.get("thumb"));
            renderJson(inforDetail);
        }else{
            Record inforDetail =  Db.findFirst("SELECT\n" +
                    "\ta.title_en as title,\n" +
                    "\ta.thumb,\n" +
                    "\ta.content_en as content,\n" +
                    "\ta.create_time,\n" +
                    "\tb.enum_id\n" +
                    "FROM\n" +
                    "\txj_information a\n" +
                    "INNER JOIN xj_enum_item b ON a.type = b.id\n" +
                    "WHERE\n" +
                    "\ta.id = ?",id);

            inforDetail.set("thumb",domain_url+"/infor/"+inforDetail.get("thumb"));
            renderJson(inforDetail);
        }

    }

    public void getIntroduce(@Para("id") int id){
        Record introduce = Db.findFirst("select id,market_name,market_name_en,introduce,introduce_en from xj_introduce where id = ?",id);
        renderJson(introduce);
    }


}
